import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },
  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true
  },
// 首页
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: {
          title: "首页",
          icon: "dashboard"
        }
      }
    ]
  },
    //管理员路由
    {
      path: "/admin",
      component: Layout,
      redirect: "/admin/list",
      name: "Admin",
      meta: { title: "管理员信息", icon: "el-icon-user" },
      children: [
        {
          path: "list",
          name: "List",
          component: () => import("@/views/admin/admin"),
          meta: { title: "管理员列表", icon: "table" }
        },
        {
          path: "add",
          name: "Add",
          component: () => import("@/views/admin/add"),
          meta: { title: "新增管理员", icon: "tree" }
        },
        {
          path: "edit/:obj",
          name: "Edit",
          component: () => import("@/views/admin/edit")
        }
      ]
    },
    // 用户路由
    {
      path: "/user",
      component: Layout,
      redirect: "/user/list",
      name: "User",
      meta: { title: "用户信息", icon: "el-icon-user" },
      children: [
        {
          path: "list",
          name: "List",
          component: () => import("@/views/user/list"),
          meta: { title: "用户列表", icon: "table" }
        },
        {
          path: "add",
          name: "Add",
          component: () => import("@/views/user/add"),
          meta: { title: "新增用户", icon: "tree" }
        },
        {
          path: "edit",
          name: "Edit",
          component: () => import("@/views/user/edit")
        },
        {
          path: "img/:_id",
          name: "Img",
          component: () => import("@/views/user/img")
        },
        {
          path: "info/:_id",
          name: "Info",
          component: () => import("@/views/user/info")
        }
      ]
    },
  // 商品
  {
    path: "/goods",
    component: Layout,
    meta: {
      title: "商品管理",
      icon: "goods"
    },
    children: [
      {
        path: "",
        name: "goods",
        component: () => import("@/views/goods/index"),
        meta: {
          title: "商品列表",
          icon: "goods"
        }
      },
      {
        path: "add",
        name: "addArticles",
        component: () => import("@/views/goods/add"),
        hidden: true,
        meta: {
          title: "添加商品"
        }
      },
      {
        path: "edit/:_id?",
        name: "editArticles",
        component: () => import("@/views/goods/edit"),
        hidden: true,
        meta: {
          title: "修改商品"
        }
      }
    ]
  },
  // 商品类型
  {
    path: "/type",
    component: Layout,
    redirect: "/type/typeOne",
    name: "Type",
    meta: { title: "类别管理", icon: "form" },
    children: [
      {
        path: "typeOne",
        name: "typeOne",
        component: () => import("@/views/type/typeOne/typeOne"),
        meta: { title: "一级列表", icon: "form" }
      },
      {
        path: "typeTwo",
        name: "typeTwo",
        component: () => import("@/views/type/typeTwo/index"),
        meta: { title: "二级分类", icon: "form" },
        children: [
          {
            path: "list",
            name: "typeTwoList",
            component: () => import("@/views/type/typeTwo/typeTwo"),
            meta: { title: "二级分类列表", icon: "form" }
          },
          {
            path: "add",
            name: "Add",
            component: () => import("@/views/type/typeTwo/typeTwoAdd"),
            meta: { title: "添加二级分类", icon: "form" }
          }
        ]
      }
    ]
  },
// 留言
  {
    path: "/message",
    component: Layout,
    redirect: "/message/index",
    name: "Message",
    meta: {
      title: "留言管理",
      icon: "el-icon-s-help"
    },
    children: [
      {
        path: "index",
        name: "Index",
        component: () => import("@/views/message/index"),
        meta: {
          title: "留言信息",
          icon: "table"
        }
      },
      {
        path: "goods",
        name: "Goods",
        component: () => import("@/views/message/goods"),
        meta: {
          title: "商品留言",
          icon: "tree"
        }
      },
      {
        path: "add/:_id?",
        name: "Add",
        component: () => import("@/views/message/add"),
        meta: {
          title: "新增留言",
          icon: "tree"
        }
      }
    ]
  },
  // 订单
  {
    path: "/orders",
    component: Layout,
    meta: { title: "订单详情", icon: "form" },
    children: [
      {
        path: "index",
        name: "Orders",
        component: () => import("@/views/orders/index"),
        meta: { title: "订单列表", icon: "form" }
      },
      {
        path: "add",
        name: "OrdersAdd",
        component: () => import("@/views/orders/add"),
        meta: { title: "新增订单", icon: "form" }
      },
      {
        path: "orders/update",
        name: "Update",
        component: () => import("@/views/orders/update")
      }
    ]
  },
  // 收藏夹
  {
    path: "/favorites",
    component: Layout,
    children: [
      {
        path: "index",
        name: "Favorites",
        component: () => import("@/views/favorites/index"),
        meta: { title: "收藏列表", icon: "form" }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  {
    path: "*",
    redirect: "/404",
    hidden: true
  }
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
